package com.kedll.library.base;

import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.CallSuper;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.LayoutManager;
import android.view.MotionEvent;

import com.bumptech.glide.Glide;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.kedll.library.R;
import com.kedll.library.adapter.MultiTypeAdapter;
import com.kedll.library.mvp.AbstractRefreshPresenter;
import com.kedll.library.mvp.RefreshingView;
import com.kedll.library.util.KeyBoardUtils;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;

import static android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import static android.support.v7.widget.RecyclerView.SCROLL_STATE_DRAGGING;
import static android.support.v7.widget.RecyclerView.SCROLL_STATE_SETTLING;

/**
 * 需要上下拉刷新的列表Activity
 *
 * @author QiQi
 * @date 15/12/18
 */
public abstract class KRefreshListActivity<V extends ViewDataBinding, T extends AbstractRefreshPresenter> extends MyBaseFragmentActivity<V, T> implements OnRefreshListener, SwipeMenuRecyclerView.LoadMoreListener, RefreshingView {
    public SwipeMenuRecyclerView list;
    protected SwipeRefreshLayout refreshLayout;

    protected MultiTypeAdapter adapter;
    protected int page = 1;

    @Override
    @CallSuper
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        list = findViewById(android.R.id.list);
        refreshLayout = findViewById(R.id.refreshLayout);
        adapter = mPresenter.getAdapter();

        list.setHasFixedSize(true);
        list.setLayoutManager(getLayoutManager());


        mPresenter.setRxAppCompatActivity(this);

        list.setAdapter(adapter);
        if (refreshLayout != null) {
            refreshLayout.setOnRefreshListener(this);
            refreshLayout.setColorSchemeResources(R.color.orangeRed, R.color.green);
        }

        list.setLoadMoreListener(this);


        list.setOnTouchListener((v, event) -> {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                KeyBoardUtils.hideKeyBoard(KRefreshListActivity.this);
            }
            return false;
        });

        list.addOnScrollListener(new RecyclerView.OnScrollListener() {
            private int mScrollState = -1;

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                this.mScrollState = newState;
                if (KRefreshListActivity.this.isFinishing() || KRefreshListActivity.this.isDestroyed()) {
                    return;
                }
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    Glide.with(KRefreshListActivity.this).resumeRequests();
                } else {
                    Glide.with(KRefreshListActivity.this).pauseRequests();
                }

            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
                if (layoutManager instanceof FlexboxLayoutManager) {
                    FlexboxLayoutManager staggeredGridLayoutManager = (FlexboxLayoutManager) layoutManager;

                    int itemCount = layoutManager.getItemCount();
                    if (itemCount <= 0) {
                        return;
                    }

                    int lastVisiblePosition = staggeredGridLayoutManager.findLastCompletelyVisibleItemPosition();
                    if (itemCount == lastVisiblePosition + 1 &&
                            (mScrollState == SCROLL_STATE_DRAGGING || mScrollState == SCROLL_STATE_SETTLING)) {
                        onLoadMore();
                    }
                }
            }
        });

    }

    /**
     * 获取布局管理器，默认是纵向线性布局
     * 扩展类可以重写此方法，自定义布局样式
     *
     * @return
     */
    protected LayoutManager getLayoutManager() {
        return new LinearLayoutManager(this);
    }

    @Override
    public void onLoadMore() {
        page++;
        mPresenter.loadMoreData();
    }

    @Override
    protected void initEvent() {
        onRefresh();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void onRefresh() {
        page = 1;
        mPresenter.refreshData();
    }

    @Override
    public void setRefreshing(boolean refreshing) {
        if (refreshLayout != null) {
            refreshLayout.setRefreshing(refreshing);
        }
    }

    @Override
    public void loadMoreFinish(boolean emptyData, boolean hasMore) {
        list.loadMoreFinish(emptyData, hasMore);
    }


    @Override
    protected void onStop() {
        super.onStop();
        if (isFinishing()) {
            list.clearOnScrollListeners();
        }
    }

}
